//
//  FriendWebService.m
//  eGiftr
//
//  Created by Phien Tram on 9/11/13.
//  Copyright (c) 2013 eGiftr. All rights reserved.
//

#import "FriendWebService.h"
#import "ServiceNameMapper.h"
#import "NSDictionary+Utility.h"
#import "SBJson.h"
#import "WebServiceDataConverter.h"

@implementation FriendWebService

- (id)initGetFriendsByUserId:(id)delegate userId:(NSString *)userId appToken:(NSString *)appToken page:(NSNumber *)page
{
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            userId, @"userId",
                            appToken, @"appToken",
                            page, @"page",
                            nil];
    self = [super initWithData:params serviceName:[ServiceNameMapper getFriendsByUserId] method:@"GET"];
    if (self) {
        self.delegate = delegate;
        
        [self request];
    }
    return self;
}

+ (id)getFriendsByUserId:(id)delegate userId:(NSString *)userId appToken:(NSString *)appToken page:(NSNumber *)page
{
    return [[self alloc] initGetFriendsByUserId:delegate userId:userId appToken:appToken page:page];
}

- (id)initGetUpcomingBirthdayFriends:(id)delegate userId:(NSString *)userId appToken:(NSString *)appToken
{
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            userId, @"userId",
                            appToken, @"appToken",
                            nil];
    self = [super initWithData:params serviceName:[ServiceNameMapper getUpcomingBirthdayFriends] method:@"GET"];
    if (self) {
        self.delegate = delegate;
        
        [self request];
    }
    return self;
}

+ (id)getUpcomingBirthdayFriends:(id)delegate userId:(NSString *)userId appToken:(NSString *)appToken
{
    return [[self alloc] initGetUpcomingBirthdayFriends:delegate userId:userId appToken:appToken];
}

- (BOOL)parse:(NSString **)error code:(NSInteger *)code
{
	id jResult = [self.receivedData JSONValue];
    NSLog(@"jResult: %@", [jResult description]);
    BOOL success = [[jResult objectForKey:@"success"] boolValue];
    if (success)
    {
        error = nil;
        NSDictionary *dataDict = [jResult objectOrNilForKey:@"data"];
        if ([self.serviceName isEqualToString:[ServiceNameMapper getFriendsByUserId]]) {
            self.result = [WebServiceDataConverter convertJsonDataToFriendList:jResult];
        } else {
            self.result = dataDict;
        }
    }
    else
    {
        NSDictionary *errorDictionary = [jResult objectOrNilForKey:@"error"];
        NSString *message = [errorDictionary objectOrNilForKey:@"errorMessage"];
        NSString *errorDetails = [errorDictionary objectOrNilForKey:@"details"];
        if (message) {
            *error = message;
        } else if (errorDetails) {
            *error = errorDetails;
        }
        NSNumber *errorCode = [errorDictionary objectOrNilForKey:@"errorCode"];
        *code = [errorCode integerValue];
        return NO;
    }
    
    
	return YES;
}

@end
